# -*- coding: UTF-8 -*-
from . import BaseController
from backend.libs import myjson as json
from django.db import connection
from backend.models import VideoType
from datetime import datetime
from backend.libs.args_util import *
from django.db.models import Q


class VideoTypeListController(BaseController):

    def get(self):
        """获取类型列表
        """
        args = PageArgs(self, 'get')
        vt = VideoType.objects.order_by('show_order')
        paginator = self.pagination(vt.all(), number_per_page=args.pageSize, m2m_fields=['price_type'])
        return self.render_json(data=paginator)


class ListAll(BaseController):

    def get(self):
        video_types = VideoType.objects.order_by('show_order').all()
        data = [ {'label': vt.name, 'value': vt.id} for vt in video_types ]
        data.append({'label': '未定义类型', 'value': -1})
        return self.render_json(data={'video_types': data})


class PriceTypeListController(BaseController):

    def get(self):
        """获取所有类目
        """
        pass
        # price_type_in_db = PriceType.objects
        # query_name = self.get_argument('name', '')
        # if query_name:
        #     price_type_in_db = price_type_in_db.filter(name__contains=query_name)
        # query_exclude_ids = self.get_argument('exclude', '')
        # print('*' * 50)
        # print(query_exclude_ids)
        # if query_exclude_ids:
        #     price_type_in_db = price_type_in_db.exclude(id__in=query_exclude_ids.split(','))

        # price_type_in_db = price_type_in_db.order_by('show_order').all()
        # print(price_type_in_db.query)
        # price_types = [ p.to_autocomplete_dict() for p in price_type_in_db ]
        # data = {
        #     "price_types": price_types
        # }
        # return self.render_json(data=data)   


class DelVideoTypePriceTypeController(BaseController):

    def post(self):
        data = json.loads(self.request.body)
        print('*' * 50)
        return self.render_json()
        # price_type_id = data.get('priceTypeId', 0)
        # print(price_type_id)
        # if price_type_id <= 0:
        #     return self.render_json(code=1000, msg='参数无效')
        # video_type_id = data.get('videoTypeId', None)
        # if not video_type_id:
        #     return self.render_json(code=1000, msg='参数无效')
        # price_type = PriceType.objects.filter(id=price_type_id).first()
        # if not price_type:
        #     return self.render_json(code=1003, msg='没有找到指定ID的价格类型')
        # video_type = VideoType.objects.filter(id=video_type_id).first()
        # if not video_type:
        #     return self.render_json(code=1003, msg='没有找到指定ID的视频类型')
        # video_type.price_type.remove(price_type)
        # return self.render_json(data=data)  


class AddVideoTypePriceTypeController(BaseController):

    def post(self):
        data = json.loads(self.request.body)
        print('*' * 50)
        print(data)
        return self.render_json()
        # price_type_id = data.get('priceTypeId', '')
        # if price_type_id.find(',') == -1:
        #     return self.render_json(code=1000, msg='此处不能新增类目，请在价格因子添加类目！')
        # print(price_type_id)
        # price_type_id = int(price_type_id.split(',')[1])
        # if price_type_id <= 0:
        #     return self.render_json(code=1000, msg='参数无效')
        # video_type_id = data.get('videoTypeId', None)
        # if not video_type_id:
        #     return self.render_json(code=1000, msg='参数无效')
        # price_type = PriceType.objects.filter(id=price_type_id).first()
        # if not price_type:
        #     return self.render_json(code=1003, msg='没有找到指定ID的价格类型')
        # video_type = VideoType.objects.filter(id=video_type_id).first()
        # if not video_type:
        #     return self.render_json(code=1003, msg='没有找到指定ID的视频类型')
        # video_type.price_type.add(price_type)
        # return self.render_json(data={'price_type':price_type.to_dict()})    


class CreateVideoTypeController(BaseController):

    def post(self):
        """创建价格类目
        """
        data = json.loads(self.request.body)
        print('*+ ' * 10)
        print(data)
        if not data['name']:
            return self.render_json(code=1001, msg='类目名称不能为空')
        video_type_id = data['id']

        if int(video_type_id) <= 0:
            video_type = VideoType()
            video_type.create_time = datetime.now()
        else:
            video_type = VideoType.objects.filter(id=video_type_id).first()
            for p in video_type.price_type.all():
                print(p)
                video_type.price_type.remove(p)
        video_type.update_time = datetime.now()
        video_type.name = data['name']
        video_type.state = data.get('state', 0)
        video_type.show_order = data.get('showOrder', 1)
        video_type.save()

        return self.render_json()

